<?php


namespace app\admin\controller;


use app\common\model\UserStatisticsModel;
use think\facade\Request;

class UserStatisController extends BaseController
{
    /**
     * 使用人数
     * @return mixed
     */
    public function use_people()
    {
        return $this->fetch();
    }

    /**
     * 登录用户
     * @return mixed
     */
    public function login_people()
    {
        return $this->fetch();
    }

    /**
     * 新增人数
     * @return mixed
     */
    public function new_people()
    {
        return $this->fetch();
    }


    /**
     * 获取数据列表
     * @return \think\response\Json
     */
    public function getlist()
    {
        $type       = Request::param("type");
        $start_time = Request::param("start_time");
        $end_time   = Request::param("end_time");
        // 分页
        $page  = Request::param("page");
        $limit = Request::param("limit");
        // 排序
        $sort_field = Request::param("field", '', 'trim');
        $sort_order = Request::param("order", '', 'trim');

        $order = "date desc";
        if (!empty($sort_field) && !empty($sort_order)) {
            $order = sprintf("%s %s", $sort_field, $sort_order);
        }

        $where = [];
        if ($type) {
            $where[] = ['stat_type', '=', is_numeric($type) ? $type : '-1'];
        }

        if ($start_time) {
            $where[] = ['date', '>=', $start_time];
        }
        if ($end_time) {
            $where[] = ['date', '<=', $end_time];
        }

        $count = UserStatisticsModel::where($where)->count("id");

        if ($count > 0) {
            $notice = UserStatisticsModel::where($where)
                ->order($order)
                ->page($page, $limit)
                ->select();

            return json(["code" => 0, "msg" => "", "data" => $notice, "count" => $count]);
        }

        return self::bulidFail('暂无数据');
    }

    /**
     * 用户统计导出excel
     * @throws \think\exception\DbException
     */
    public function excel()
    {
        set_time_limit(0);
        $type       = Request::param("type", 0, 'intval');
        $start_time = Request::param("start_time");
        $end_time   = Request::param("end_time");

        if (!in_array($type, [1, 2, 3])) {
            return self::bulidFail('参数错误');
        }

        $where = [];
        if ($type) {
            $where[] = ['stat_type', '=', is_numeric($type) ? $type : '-1'];
        }

        if ($start_time) {
            $where[] = ['date', '>=', $start_time];
        }
        if ($end_time) {
            $where[] = ['date', '<=', $end_time];
        }


        $statis_model = UserStatisticsModel::where($where);

        $list = [];
        $statis_model->chunk(500, function ($querys) use (&$list, $type) {
            foreach ($querys as $query) {
                $data   = array();
                $data[] = $query->date;
                $data[] = $query->android;
                $data[] = $query->ios;
                $data[] = $query->web;
                if ($type != 2) {
                    $data[] = $query->h5;
                }
                $list[] = $data;
            }
        }, 'date', 'desc');

        $file_title = ['日期', '安卓APP', '苹果APP', 'WEB'];
        if ($type != 2) {
            array_push($file_title, 'H5');
        }
        switch ($type) {
            case 1:
                $file_name = '使用人数';
                break;
            case 2:
                $file_name = '登录用户';
                break;
            case 3:
                $file_name = '新增用户';
                break;
        }

        // 记录日志
        parent::addAdminLog("导出{$file_name}报表");

        $file_name = $file_name . date('Y年m月d日H点');
        return export_excel($list, $file_title, $file_name, $file_name);
    }

}
